//
//  WQMainViewController.swift
//  WeiBo
//
//  Created by 张德华 on 7/23/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQMainViewController: UITabBarController {
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabBar: WQTabBar = WQTabBar()
        
        tabBar.closure = {
            
            let composeView = WQComposeView()
             composeView.composeViewShow(self)
            
            
            
            
        }
        
        
        setValue(tabBar, forKey: "tabBar")
        
        ///------ 添加控制器
        addChildViewController(WQHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(WQMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(WQDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(WQProfileViewController(), title: "我", imageName: "tabbar_profile")
        
        
        showNewFutura()
        
    }
    
    
    
    // MARK: - ************封装快速创建控制器的方法
    func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    
    
    
    
}


// MARK: - ************新特性界面
extension WQMainViewController {
    
    func showNewFutura() {
        
        if !WQUserAccountModel.sharedUserAccount.isLogin {
            return
        }
        
        let v = isNewVersion ? WQNewFuturaView() : WQWelcomeView()
        
        if v.isKindOfClass(WQNewFuturaView) {
            
            
        }
        
        view.addSubview(v)
        
        
    }
    
    ///------ 判断是否是新版本
    private var isNewVersion: Bool {
        ///------ 1.获取当前版本
        let currentVersion = NSBundle.wq_currentVersion
        
        ///------ 2.获取之前版本,
        let versionKey = "wq.zhang.versionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        
        ///------ 3.当前版本号保存到用户偏好
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        
        print("是否新版本: \(currentVersion > sandboxVersion)")
        
        ///------ 4.返回比较的版本
        return currentVersion > sandboxVersion
        
    }
    
    
    private var version: String {
        
        guard let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String else {
            
            return "0.0.0"
        }
        return currentVersion
    }
    
    
    
}

